package com.answeringplayer.Bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.Serializable;

@Component
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Response<T> implements Serializable {

    private Integer code;    // 状态码
    private String message; // 提示信息
    private T data;         // 响应数据

    // 成功响应的静态方法
    public static <T> Response<T> success(T data) {
        return new Response<>(200, "success", data);
    }

    public static <T> Response<T> success(String message, T data) {
        return new Response<>(200, message, data);
    }

    // 错误响应的静态方法
    public static <T> Response<T> error(Integer code, String message) {
        return new Response<>(code, message, null);
    }

    public static <T> Response<T> error(String message) {
        return new Response<>(500, message, null);
    }

    // 便捷的成功方法（无数据）
    public static Response<Void> success() {
        return new Response<>(200, "success", null);
    }

    public static Response<Void> success(String message) {
        return new Response<>(200, message, null);
    }

    // 判断是否成功的便捷方法
    public boolean isSuccess() {
        return code != null && code == 200;
    }
}